package com.huawei.esdk.uc.sametime.chatwindow;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Composite;

import com.huawei.esdk.uc.jna.Constants;
import com.huawei.esdk.uc.jna.Constants.AccountType;
import com.huawei.esdk.uc.sametime.models.Participant;
import com.huawei.esdk.uc.sametime.models.ParticipantList;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.util.PluginUtil;
import com.huawei.esdk.uc.sametime.widgets.EsdkUCConvWindow;
import com.ibm.collaboration.realtime.chatwindow.ChatWindowHandler;
import com.ibm.collaboration.realtime.chatwindow.addon.PopupAddOn;
import com.ibm.collaboration.realtime.people.PeopleUtil;
import com.ibm.collaboration.realtime.people.Person;

/**
 * 聊天窗口上的按钮扩展类
 * @author y00175154
 * 
 */
public class ChatwindowConvPopupAddOn implements PopupAddOn
{
    private Logger logger = Logger.getLogger(ChatwindowConvPopupAddOn.class);

    private Person[] participants;

    private ParticipantList pList;

    private boolean isConf = false;

    private boolean isVideo = false;

    public Composite createControl(final ChatWindowHandler handler,
            Composite parent)
    {
        logger.debug("createControl");
        participants = handler.getParticipants();
        Participant tempParticipant = null;
        pList = new ParticipantList();
        String ucAccount = null;
        for (int i = 0; i < participants.length; i++)
        {

            if (PeopleUtil.isMe(participants[i]))
            {
                continue;
            }
            tempParticipant = new Participant();
            ucAccount = PluginUtil.getUCAccountFromContactID(participants[i]
                    .getContactId());
            tempParticipant.setUcAccount(ucAccount);
            tempParticipant.setNumber(ucAccount);
            tempParticipant.setCallNumType(AccountType.UC_ACCOUNT);
            pList.addparticipants(tempParticipant);
        }

        EsdkUCConvWindow window = EsdkUCConvWindow.getInstance();
        window.setVideoEnabled(isVideo);
        if (isConf && pList.getparticipants().size() >= 2)
        {
            window.setConfEnabled(isConf);
            Person person = PeopleUtil.getLocalPerson();
            Participant participant = new Participant();
            participant.setContactId(person.getContactId());
            participant.setNumber(PluginUtil.getUCAccountFromContactID(person
                    .getContactId()));
            participant.setCallNumType(Constants.AccountType.UC_ACCOUNT);
            pList.getparticipants().add(0, participant);
        }
        window.createUCConvWindow(pList, parent, handler);
        window.openChatContent();

        return window.getComposite();
    }

    private void setConvEnabledProps()
    {
        isConf = GlobalConfiguration.getInstance().isConf();
        isVideo = GlobalConfiguration.getInstance().isVideo();
    }

    public int getInitialSize()
    {
        return getCompositeSize();
    }

    private int getCompositeSize()
    {
        logger.debug("getCompositeSize");
        setConvEnabledProps();
        if (isVideo)
        {
            return 370;
        }
        return 90;
    }

    @Override
    public String getName()
    {
        return "";
    }

}
